package org.jetlinks.pro.auth.tenant;

import lombok.AllArgsConstructor;
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
import org.jetlinks.pro.assets.DefaultAsset;
import org.jetlinks.pro.auth.entity.OrganizationEntity;
import org.jetlinks.pro.assets.AssetType;
import org.jetlinks.pro.tenant.AssetSupplier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Component
@AllArgsConstructor
public class OrganizationAssetSupplier implements AssetSupplier {

    private final ReactiveRepository<OrganizationEntity, String> repository;

    @Override
    public List<AssetType> getTypes() {
        return Arrays.asList(OrganizationAssetType.values());
    }

    @Override
    @SuppressWarnings("unchecked")
    public Flux<DefaultAsset> getAssets(AssetType type, Collection<?> assetId) {
        return repository
            .findById((Collection<String>) assetId)
            .map(org -> new DefaultAsset(org.getId(), org.getName(), OrganizationAssetType.organization));
    }
}
